<script setup lang="ts">
// 使用统一的markdown工具
import { parseMarkdown } from '../utils/markdown'
 import { ElMessage } from 'element-plus'
import { DocumentCopy, Star, StarFilled, ArrowDown } from '@element-plus/icons-vue'
import { ref } from 'vue'

// 定义props
const props = defineProps<{
  message: {
    id?: string
    role: 'system' | 'user' | 'assistant'
    content: string
    timestamp?: number
    created_at?: string | number
    reasoning?: string  // 思考内容
    isThinking?: boolean  // 是否正在思考
    thinkingComplete?: boolean  // 思考是否完成
  }
  loading?: boolean
}>()

// 思考内容展开/收起状态
const reasoningExpanded = ref(false)

// 格式化时间
const formatTime = (timestamp?: string | number): string => {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
}

// 复制消息内容
const copyMessage = (): void => {
  const text = props.message.content
  navigator.clipboard.writeText(text)
    .then(() => {
      ElMessage({
        message: '已复制到剪贴板',
        type: 'success',
        duration: 2000
      })
    })
    .catch(() => {
      ElMessage.error('复制失败')
    })
}

// 复制思考内容
const copyReasoning = (): void => {
  const text = props.message.reasoning || ''
  navigator.clipboard.writeText(text)
    .then(() => {
      ElMessage({
        message: '思考内容已复制到剪贴板',
        type: 'success',
        duration: 2000
      })
    })
    .catch(() => {
      ElMessage.error('复制失败')
    })
}

// 切换思考内容展开状态
const toggleReasoning = (): void => {
  reasoningExpanded.value = !reasoningExpanded.value
}
</script>

<template>
  <!-- QQ风格：头像和气泡分离布局 -->
  <div class="message-container" :class="{ 'user-message': message.role === 'user', 'assistant-message': message.role === 'assistant' }">
    
    <!-- 用户消息：头像在右侧，消息内容也在右侧 -->
    <template v-if="message.role === 'user'">
      <div class="message-content-wrapper">
        <div class="message-info">
          <span class="timestamp" v-if="message.timestamp || message.created_at">
            {{ formatTime(message.timestamp || message.created_at) }}
          </span>
          <span class="username">您</span>
        </div>
        <div class="message-bubble user-bubble">
          <div class="user-content">{{ message.content }}</div>
        </div>
      </div>
      <div class="avatar-wrapper">
        <el-avatar :size="40" class="user-avatar">
          <span>您</span>
        </el-avatar>
      </div>
    </template>
    
    <!-- AI助手消息：头像在左侧 -->
    <!-- 在AI助手消息模板中 -->
    <template v-else>
      <div class="avatar-wrapper">
        <el-avatar :size="40" class="assistant-avatar">
          <span>AI</span>
        </el-avatar>
      </div>
      <div class="message-content-wrapper">
        <div class="message-info">
          <span class="username">AI助手</span>
          <span class="timestamp" v-if="message.timestamp || message.created_at">
            {{ formatTime(message.timestamp || message.created_at) }}
          </span>
        </div>
        
        <!-- 思考内容区域 - 即使没有主要内容也要显示 -->
        <div v-if="message.reasoning || message.isThinking" class="reasoning-container">
          <div class="reasoning-header" @click="toggleReasoning">
            <div class="reasoning-icon">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 2L13.09 8.26L20 9L13.09 9.74L12 16L10.91 9.74L4 9L10.91 8.26L12 2Z" fill="currentColor"/>
              </svg>
            </div>
            <span class="reasoning-title">
              {{ message.isThinking ? '思考中...' : `已深度思考 (用时 ${message.reasoning ? Math.ceil(message.reasoning.length / 10) : 0} 秒)` }}
            </span>
            <el-icon class="reasoning-toggle" :class="{ 'expanded': reasoningExpanded }">
              <ArrowDown />
            </el-icon>
          </div>
          
          <div v-if="reasoningExpanded" class="reasoning-content">
            <div v-if="message.isThinking && !message.reasoning" class="thinking-indicator">
              <div class="thinking-dots">
                <span></span><span></span><span></span>
              </div>
            </div>
            <div v-else class="reasoning-text" v-html="parseMarkdown(message.reasoning || '')"></div>
            
            <!-- 思考内容操作按钮 -->
            <div v-if="!message.isThinking && message.reasoning" class="reasoning-actions">
              <el-tooltip content="复制思考内容" placement="top">
                <el-button class="action-btn" size="small" @click="copyReasoning">
                  <el-icon><DocumentCopy /></el-icon>
                </el-button>
              </el-tooltip>
            </div>
          </div>
        </div>
        
        <!-- 主要回复内容 -->
        <div class="message-bubble assistant-bubble">
          <div v-if="loading" class="typing-indicator">
            <span></span><span></span><span></span>
          </div>
          <div v-else class="assistant-content" v-html="parseMarkdown(message.content)"></div>
        </div>
        
        <!-- 操作按钮 -->
        <div class="message-actions" v-if="!loading">
          <el-tooltip content="复制消息" placement="top">
            <el-button class="action-btn" size="small" @click="copyMessage">
              <el-icon><DocumentCopy /></el-icon>
            </el-button>
          </el-tooltip>
          <el-tooltip content="点赞" placement="top">
            <el-button class="action-btn" size="small">
              <el-icon><StarFilled /></el-icon>
            </el-button>
          </el-tooltip>
          <el-tooltip content="点踩" placement="top">
            <el-button class="action-btn" size="small">
              <el-icon><Star /></el-icon>
            </el-button>
          </el-tooltip>
        </div>
      </div>
    </template>
  </div>
</template>

<style lang="scss" scoped>
// 颜色变量 - 增强用户消息对比度
$user-bubble-bg: #1976D2; // 使用更深的蓝色背景
$assistant-bubble-bg: #ffffff;
$user-text-color: #ffffff; // 纯白色文字
$assistant-text-color: #333333;
$border-radius: 12px;
$transition-speed: 0.3s;
$code-block-bg: #282c34;
$code-block-border: #6366F1;
$code-text-color: #e6e6e6;

// QQ风格消息容器
.message-container {
  display: flex;
  margin-bottom: 20px;
  max-width: 100%;
  animation: fadeIn 0.3s ease-out;
  
  // 用户消息：右对齐，整体布局在右侧
  &.user-message {
    justify-content: flex-end;
    padding-left: 20%;
    
    .message-content-wrapper {
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      margin-right: 12px;
      
      .message-info {
        margin-bottom: 4px;
        text-align: right;
        display: flex;
        align-items: center;
        gap: 8px;
        
        .username {
          color: #666;
          font-size: 0.85rem;
          font-weight: 600;
        }
        
        .timestamp {
          color: #999;
          font-size: 0.8rem;
        }
      }
    }
  }
  
  // AI助手消息：左对齐
  &.assistant-message {
    justify-content: flex-start;
    padding-right: 15%;
    
    .message-content-wrapper {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      margin-left: 12px;
      flex: 1;
      
      .message-info {
        margin-bottom: 4px;
        display: flex;
        align-items: center;
        gap: 8px;
        
        .username {
          color: #666;
          font-size: 0.85rem;
          font-weight: 600;
        }
        
        .timestamp {
          color: #999;
          font-size: 0.8rem;
        }
      }
    }
  }
}

// 头像样式
.avatar-wrapper {
  flex-shrink: 0;
  
  .user-avatar {
    background: linear-gradient(135deg, #1976D2 0%, #1565C0 100%);
    color: white;
    font-weight: 700;
    font-size: 16px;
    box-shadow: 0 3px 10px rgba(25, 118, 210, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.9);
  }
  
  .assistant-avatar {
    background: linear-gradient(135deg, #6366F1 0%, #8B5CF6 100%);
    color: white;
    font-weight: 700;
    font-size: 14px;
    box-shadow: 0 3px 10px rgba(99, 102, 241, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.9);
  }
}

// 消息气泡样式
.message-bubble {
  max-width: 100%;
  padding: 14px 18px;
  border-radius: $border-radius;
  position: relative;
  word-break: break-word;
  transition: all $transition-speed;
  
  // 用户气泡 - 增强对比度和可读性
  &.user-bubble {
    background: $user-bubble-bg;
    color: $user-text-color;
    border-radius: 18px 18px 4px 18px;
    box-shadow: 
      0 4px 15px rgba(25, 118, 210, 0.3),
      0 2px 8px rgba(25, 118, 210, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.2);
    
    // QQ风格的小尾巴
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      right: -6px;
      width: 12px;
      height: 12px;
      background: $user-bubble-bg;
      border-radius: 0 0 12px 0;
      transform: rotate(-45deg);
      border: 1px solid rgba(255, 255, 255, 0.2);
      border-top: none;
      border-left: none;
    }
    
    &:hover {
      box-shadow: 
        0 6px 20px rgba(25, 118, 210, 0.4),
        0 3px 12px rgba(25, 118, 210, 0.3);
      transform: translateY(-1px);
    }
  }
  
  // AI助手气泡
  &.assistant-bubble {
    background: $assistant-bubble-bg;
    color: $assistant-text-color;
    border-radius: 4px 18px 18px 18px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.1);
    
    // QQ风格的小尾巴
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: -6px;
      width: 12px;
      height: 12px;
      background: $assistant-bubble-bg;
      border: 1px solid rgba(99, 102, 241, 0.1);
      border-radius: 0 0 0 12px;
      transform: rotate(45deg);
      border-top: none;
      border-right: none;
    }
    
    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
  }
}

// 用户内容样式 - 增强文字可读性
.user-content {
  font-size: 0.95rem;
  line-height: 1.6;
  white-space: pre-wrap;
  color: #ffffff; // 纯白色文字
  font-weight: 500;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); // 添加文字阴影增强可读性
  letter-spacing: 0.3px; // 增加字符间距
}

// AI助手内容样式
.assistant-content {
  font-size: 0.95rem;
  line-height: 1.6;
  
  // 修复代码块样式
  :deep(.code-block) {
    background-color: $code-block-bg;
    border-radius: 8px;
    margin: 12px 0;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border-left: 3px solid $code-block-border;
    position: relative;
    
    // 修复语言标签位置
    &::before {
      content: attr(data-language);
      position: absolute;
      top: 8px;
      right: 12px;
      padding: 4px 8px;
      font-size: 0.7rem;
      font-weight: 600;
      color: rgba(255, 255, 255, 0.9);
      background: rgba(99, 102, 241, 0.8);
      border-radius: 4px;
      z-index: 10;
      letter-spacing: 0.5px;
      font-family: 'JetBrains Mono', 'Fira Code', Consolas, monospace;
    }
    
    code {
      display: block;
      padding: 16px;
      padding-top: 40px; // 为语言标签留出空间
      overflow-x: auto;
      font-family: 'JetBrains Mono', 'Fira Code', Consolas, monospace;
      color: $code-text-color;
      font-size: 0.85rem;
      line-height: 1.5;
      background: transparent;
      
      // 滚动条样式
      &::-webkit-scrollbar {
        height: 6px;
      }
      
      &::-webkit-scrollbar-track {
        background: rgba(255, 255, 255, 0.1);
        border-radius: 3px;
      }
      
      &::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.3);
        border-radius: 3px;
        
        &:hover {
          background: rgba(255, 255, 255, 0.5);
        }
      }
    }
  }
  
  // 其他markdown元素样式
  :deep(p) {
    margin: 8px 0;
    &:first-child { margin-top: 0; }
    &:last-child { margin-bottom: 0; }
  }
  
  :deep(ul), :deep(ol) {
    margin: 8px 0;
    padding-left: 20px;
  }
  
  :deep(blockquote) {
    border-left: 3px solid #6366F1;
    padding-left: 12px;
    margin: 12px 0;
    color: #666;
    font-style: italic;
  }
}

// 打字指示器
.typing-indicator {
  display: flex;
  align-items: center;
  padding: 8px 0;
  
  span {
    display: inline-block;
    width: 8px;
    height: 8px;
    background-color: rgba(99, 102, 241, 0.7);
    border-radius: 50%;
    margin: 0 2px;
    animation: typing 1.5s infinite ease-in-out;
    
    &:nth-child(1) { animation-delay: 0s; }
    &:nth-child(2) { animation-delay: 0.2s; }
    &:nth-child(3) { animation-delay: 0.4s; }
  }
}

// 操作按钮
.message-actions {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity 0.3s ease;
  
  .action-btn {
    height: 28px;
    width: 28px;
    padding: 4px;
    border: none;
    color: #6366F1;
    background-color: rgba(99, 102, 241, 0.1);
    border-radius: 50%;
    
    &:hover {
      background-color: #6366F1;
      color: white;
    }
  }
}

// 鼠标悬停显示操作按钮
.message-container:hover .message-actions {
  opacity: 1;
}

// 动画
@keyframes typing {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.8;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 思考内容样式
.reasoning-container {
  margin-bottom: 12px;
  border: 1px solid #e1e5e9;
  border-radius: 8px;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  overflow: hidden;
  transition: all 0.3s ease;
  
  &:hover {
    border-color: #d1d9e0;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  }
}

.reasoning-header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.2s ease;
  
  &:hover {
    background: rgba(255, 255, 255, 0.5);
  }
}

.reasoning-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  margin-right: 8px;
  color: #6366f1;
  animation: sparkle 2s ease-in-out infinite;
}

@keyframes sparkle {
  0%, 100% { transform: scale(1) rotate(0deg); opacity: 0.8; }
  50% { transform: scale(1.1) rotate(180deg); opacity: 1; }
}

.reasoning-title {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 500;
  color: #374151;
}

.reasoning-toggle {
  transition: transform 0.3s ease;
  color: #6b7280;
  
  &.expanded {
    transform: rotate(180deg);
  }
}

.reasoning-content {
  padding: 0 16px 16px;
  border-top: 1px solid #e5e7eb;
  background: #ffffff;
}

.thinking-indicator {
  display: flex;
  align-items: center;
  padding: 16px 0;
}

.thinking-dots {
  display: flex;
  gap: 4px;
  
  span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #6366f1;
    animation: thinking 1.4s ease-in-out infinite both;
    
    &:nth-child(1) { animation-delay: -0.32s; }
    &:nth-child(2) { animation-delay: -0.16s; }
    &:nth-child(3) { animation-delay: 0s; }
  }
}

@keyframes thinking {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1.2);
    opacity: 1;
  }
}

.reasoning-text {
  font-size: 0.9rem;
  line-height: 1.6;
  color: #4b5563;
  
  // 为思考内容中的代码块添加特殊样式
  :deep(pre) {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    padding: 12px;
    margin: 8px 0;
    font-size: 0.85rem;
  }
  
  :deep(code) {
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.85rem;
  }
}

.reasoning-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #f3f4f6;
}
</style>